﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using System.Windows;
using Microsoft.Xna.Framework.Input.Touch;

namespace SheepClock
{
    public class Sheep
    {
        Texture2D mTexture;
        Vector2 mPosition;
        float mVelocity;

        long lastTimeUpdated;
        long lastTimeTextureChanged;
        float changeAnimationPeriod;
        bool legsClose;

        public Sheep()
            : this(5f)
        {

        }

        public bool BelongsToHerd
        {
            get;
            set;
        }

        public bool IsOutOfTheScreen
        {
            get;
            set;
        }

        private bool DidBaa
        {
            get;
            set;
        }

        public Sheep(float velocity)
        {
            mTexture = GlobalParams.contentManager.Load<Texture2D>("sheep_legsfar");
            legsClose = false;
            lastTimeTextureChanged = Environment.TickCount;
            mVelocity = velocity;
            changeAnimationPeriod = 3000 / mVelocity;
            mPosition = new Vector2(-mTexture.Width, GlobalParams.ScreenHeight - mTexture.Height + 4);
        }

        public void Update()
        {
            if (lastTimeUpdated == 0)
                lastTimeUpdated = Environment.TickCount;

            long elapsedTime = Environment.TickCount - lastTimeUpdated;
            mPosition = new Vector2(mPosition.X + mVelocity * elapsedTime / 100f, GlobalParams.ScreenHeight - mTexture.Height + 4);

            if (mPosition.X >= GlobalParams.ScreenWidth)
            {
                if (BelongsToHerd)
                {
                    IsOutOfTheScreen = true;
                    return;
                }
                else
                    mPosition.X = -mTexture.Width;
            }

            float distanceFromFence = Math.Abs(GlobalParams.ScreenWidth / 2 - (mPosition.X + mTexture.Width / 2));
            if (distanceFromFence < mTexture.Width)
            {
                if (!DidBaa && BelongsToHerd)
                {
                    DidBaa = true;
                    MainPage.sheepSound.Play();
                }

                if (legsClose)
                {
                    mTexture = GlobalParams.contentManager.Load<Texture2D>("sheep_legsfar");
                    legsClose = false;
                }
                mPosition.Y = GlobalParams.ScreenHeight - mTexture.Height + 4
                    + (distanceFromFence - mTexture.Height - 20)
                    + (mTexture.Width - distanceFromFence) / 3;
            }
            else if (Environment.TickCount - lastTimeTextureChanged > changeAnimationPeriod)
            {
                if (legsClose)
                    mTexture = GlobalParams.contentManager.Load<Texture2D>("sheep_legsfar");
                else
                    mTexture = GlobalParams.contentManager.Load<Texture2D>("sheep_legsclose");
                legsClose = !legsClose;
                lastTimeTextureChanged = Environment.TickCount;
            }

            lastTimeUpdated = Environment.TickCount;
        }

        public virtual void Draw(SpriteBatch spriteBatch, Color tintColor)
        {
            spriteBatch.Draw(mTexture, mPosition, tintColor);
        }

        public bool ContainsTouch(TouchLocation touch)
        {
            if (new Rectangle((int)mPosition.X, (int)mPosition.Y, (int)mTexture.Width, (int)mTexture.Height).Contains((int)touch.Position.X, (int)touch.Position.Y))
                return true;
            return false;
        }

        public void SetDelayThroughXPosition(int pixelsDelay)
        {
            mPosition.X -= pixelsDelay;
        }

    }
}